﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using smp.ButterflyCakes.Models.Data;

namespace smp.ButterflyCakes.Models.FormModels.Order
{
    public class CakeFlavourModel
    {
        [Required]
        public Guid OrderId { get; set; }

        [Required]
        public Guid ComponentId { get; set; }

        [Required]
        public Guid? CakeFlavourTypeId { get; set; }

        [Required]
        public Guid? CakeFlavourId { get; set; }

        public Guid StageId { get; set; }

        [Required]
        public string StageName { get; set; }

        public ComponentType ComponentType { get; set; }

        public bool IsCarved { get; set; }

        public SelectItem SelectCakeFlavourTypes { get; private set; }

        public void AddFlavourTypes(List<CakeFlavourTypeItem> types)
        {
            SelectCakeFlavourTypes = new SelectItem("CakeFlavourTypeId", true);
            foreach (var item in types)
            {
                if (ComponentType == ComponentType.CustomCupCakes && !item.IsCompatibleWithCupCakes)
                {
                    SelectCakeFlavourTypes.AddOption(new SelectOption(item.Name, item.Id, CakeFlavourTypeId == item.Id, true, "Not compatible with cup cakes"));
                }
                else if (ComponentType == ComponentType.Custom3DCake && !item.IsCompatibleWith3DCakes)
                {
                    SelectCakeFlavourTypes.AddOption(new SelectOption(item.Name, item.Id, CakeFlavourTypeId == item.Id, true, "Not compatible with 3D cakes"));
                }
                else if (ComponentType == ComponentType.CustomGiantCupCake && !item.IsCompatibleWithGiantCakes)
                {
                    SelectCakeFlavourTypes.AddOption(new SelectOption(item.Name, item.Id, CakeFlavourTypeId == item.Id, true, "Not compatible with giant cup cakes"));
                }
                else if (ComponentType == ComponentType.CustomCake && IsCarved && !item.CanCarve)
                {
                    SelectCakeFlavourTypes.AddOption(new SelectOption(item.Name, item.Id, CakeFlavourTypeId == item.Id, true, "Not compatible with carved cakes"));
                }
                else
                {
                    SelectCakeFlavourTypes.AddOption(new SelectOption(item.Name, item.Id, CakeFlavourTypeId == item.Id));
                }
            }
        }

        public SelectItem SelectCakeFlavour { get; private set; }

        public void AddFlavours(List<CakeFlavourItem> items)
        {
            if(!CakeFlavourTypeId.HasValue || items == null)
            {
                SelectCakeFlavour = new SelectItem("CakeFlavourId");
                SelectCakeFlavour.AddOption(new SelectOption("Please select a flavour type", null));
                return;
            }

            SelectCakeFlavour = new SelectItem("CakeFlavourId", true);
            var selectableCount = 0;
            foreach(var flavour in items)
            {
                var isSelected = CakeFlavourId.HasValue && CakeFlavourId == flavour.Id;
                if (!flavour.CanCarve && IsCarved)
                {
                    SelectCakeFlavour.AddOption(new SelectOption(flavour.Name, flavour.Id, isSelected, true, "Not suitable for carving."));
                }
                else
                {
                    SelectCakeFlavour.AddOption(new SelectOption(flavour.Name, flavour.Id, isSelected));
                    selectableCount++;
                }
            }
            if (!CakeFlavourId.HasValue && selectableCount == 1)
            {
                SelectCakeFlavour.Options.Find(x => !x.IsDisabled).IsSelected = true;
            }
        }
    }
}
